
import { InjectRepository } from '@nestjs/typeorm';
import { Repository ,Not ,In,Like} from 'typeorm';
import { Injectable } from '@nestjs/common';
import { ResultData } from 'src/common/utils/result';
import { CreateProfessionalDto, UpdateProfessionalDto, QueryProfessionalDto } from './dto/professional.dto';
import { ProfessionalEntity } from './entities/professional.entity';

@Injectable()
export class ProfessionalService {
constructor(
    @InjectRepository(ProfessionalEntity)
    private readonly professionalEntityRep: Repository<ProfessionalEntity>,
) {}
    async create(createProfessionalDto: CreateProfessionalDto) {
        const res = await this.professionalEntityRep.save(createProfessionalDto);
        return ResultData.ok(res);
    }

    async findAll(query :QueryProfessionalDto ) {
        const entity = this.professionalEntityRep.createQueryBuilder('entity');
        entity.where({ delFlag: '0'});
        if (query.orderByColumn && query.isAsc) {
          const key = query.isAsc === 'ascending' ? 'ASC' : 'DESC';
          entity.orderBy(`entity.${query.orderByColumn}`, key);
        }
        entity.skip(query.pageSize * (query.pageNum - 1)).take(query.pageSize);
        const [list, total] = await entity.getManyAndCount();

        return ResultData.ok({
            list,
            total,
        });
    }

    async findOne(id: number) {
        const res = await this.professionalEntityRep.findOne({
            where: {
                delFlag: '0',
                proId: id,
            },
        });
        return ResultData.ok(res);
    }

    async update(updateProfessionalDto: UpdateProfessionalDto) {
        const res = await this.professionalEntityRep.update({  proId: updateProfessionalDto.proId }, updateProfessionalDto);
        return ResultData.ok({value:res.affected >= 1});
    }

    async remove(proIds: number[]) {
        const res = await this.professionalEntityRep.update(
            { proId: In(proIds) },
            {
                delFlag: '1',
            },
        );
        return ResultData.ok({value:res.affected >= 1});
    }



}
